package com.framwork.utils.helper.recyleview.divider;

import android.content.Context;
import android.graphics.Rect;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

/**
 * @ProjectName: HeHaCatApp
 * @Package: com.framwork.utils.helper.recyleview.divider
 * @ClassName: StaggeredDividerItemDecoration
 * @Description: java类作用描述
 * @Author: ycDeng
 * @CreateDate: 2021/2/2 16:43
 * @UpdateUser: 更新者
 * @UpdateDate: 2021/2/2 16:43
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
public class StaggeredDividerItemDecoration extends RecyclerView.ItemDecoration {
    private Context context;
    private int spacDistance = 10;//间距
    private int spanCount = 2;//列数

    public StaggeredDividerItemDecoration(Context context, int spanCount, int spanceDistance) {
        this.context = context;
        this.spacDistance = spanceDistance;
        this.spanCount = spanCount;
    }

    @Override
    public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
        StaggeredGridLayoutManager.LayoutParams params = (StaggeredGridLayoutManager.LayoutParams) view.getLayoutParams();
        int spanIndex = params.getSpanIndex();
        if (spanIndex % spanCount == 0) {
            outRect.left = 0;
        } else {
            outRect.left = spacDistance;
        }
        outRect.bottom = spacDistance;
    }
}
